<?php

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * banner管理
 */
class Banner extends BaseModel
{
    use HasFactory;

    const CREATED_AT = 'create_time';
    const UPDATED_AT = 'change_time';


    protected $table = 'banner';


    /**
     * 获取banner 图片
     * @param 类型  显示位置 0全部  1 首页     2 商城
     */
    public static function getBannerList($type)
    {
        return self::select('name', 'img', 'link')
            ->where('is_play', 1)
            ->where('is_del', 1)
            ->where(function ($query) use ($type) {
                if ($type) {
                    $query->where('type', $type);
                }
            })
            ->orderByDesc('sort')
            ->get()
            ->toArray(); //banenr图片
    }

    /**
     * 获取banner mode
     */
    public function getBannerMode($id = null)
    {
        $data = [
            ['id' => 1, 'name' => '活动报名'],
            ['id' => 2, 'name' => '答题活动'],
            ['id' => 3, 'name' => '翻奖活动'],
            ['id' => 4, 'name' => '投票活动'],
            ['id' => 5, 'name' => '图片直播'],
            ['id' => 6, 'name' => '新闻动态'],
            ['id' => 7, 'name' => '场馆预约'],
            ['id' => 8, 'name' => '在线办证'], //无需获取对应数据
        ];

        if ($id) {
            $data = array_column($data, 'name', 'id');
            return !empty($data[$id]) ? $data[$id] : null;
        }
        return $data;
    }


    /**
     * 获取跳转内容的方式的数据
     * @param mode   1 新书  2 馆藏书
     * @param keywords string 搜索关键词
     */
    public function getBannerModeData($mode, $keywords)
    {
        $data = [];
        switch ($mode) {
            case 1: // 活动报名
                $model = new Activity();
                $data = $model->select('id', 'title')->where('is_del', 1)->where(function ($query) use ($keywords) {
                    if ($keywords) {
                        $query->where('title', 'like', "%$keywords%");
                    }
                })->orderByDesc('id')
                    ->get()
                    ->toArray();
                break;
            case 2: // 答题活动
                $model = new AnswerActivity();
                $data = $model->select('id', 'title')->where('is_del', 1)->where(function ($query) use ($keywords) {
                    if ($keywords) {
                        $query->where('title', 'like', "%$keywords%");
                    }
                })->orderByDesc('id')
                    ->get()
                    ->toArray();
                break;
            case 3: // 翻奖活动
                $model = new TurnActivity();
                $data = $model->select('id', 'title')->where('is_del', 1)->where(function ($query) use ($keywords) {
                    if ($keywords) {
                        $query->where('title', 'like', "%$keywords%");
                    }
                })->orderByDesc('id')
                    ->get()
                    ->toArray();
                break;
            case 4: // 投票活动
                $model = new ContestActivity();
                $data = $model->select('id', 'title')->where('is_del', 1)->where(function ($query) use ($keywords) {
                    if ($keywords) {
                        $query->where('title', 'like', "%$keywords%");
                    }
                })->orderByDesc('id')
                    ->get()
                    ->toArray();
                break;
            case 5: // 图片直播
                $model = new PictureLive();
                $data = $model->select('id', 'title')->where('is_del', 1)->where(function ($query) use ($keywords) {
                    if ($keywords) {
                        $query->where('title', 'like', "%$keywords%");
                    }
                })->orderByDesc('id')
                    ->get()
                    ->toArray();
                break;
            case 6: // 新闻动态
                $model = new News();
                $data = $model->select('id', 'title')->where('is_del', 1)->where(function ($query) use ($keywords) {
                    if ($keywords) {
                        $query->where('title', 'like', "%$keywords%");
                    }
                })->orderByDesc('id')
                    ->get()
                    ->toArray();
                break;
            case 7: // 场馆预约
                $model = new Reservation();
                $data = $model->select('id', 'name as title')->where('is_del', 1)->where(function ($query) use ($keywords) {
                    if ($keywords) {
                        $query->where('title', 'like', "%$keywords%");
                    }
                })->orderByDesc('id')
                    ->get()
                    ->toArray();
                break;
            case 8: // 在线办证
                $data = [];
                break;
            default:
                $data = [];
        }
        return $data;
    }


    /**
     *获取列表
     * @param limit int 分页大小
     * @param skip_way 跳转方式  1、H5链接，2、应用内部  3 不跳转（默认3）
     * @param is_play    是否发布   1 发布  2 撤销
     * @param keywords string 搜索关键词
     */
    public function lists($search_data = [])
    {
        $keywords = $search_data['keywords'];
        $skip_way = $search_data['skip_way'];
        $is_play = $search_data['is_play'];
        $limit = !empty($search_data['limit']) ? $search_data['limit'] : 10;

        $res = $this->where(function ($query) use ($keywords) {
            if ($keywords) {
                $query->where('name', 'like', "%$keywords%");
            }
        })->where(function ($query) use ($skip_way, $is_play) {
            if ($skip_way) {
                $query->where('skip_way', $skip_way);
            }
            if ($is_play) {
                $query->where('is_play', $is_play);
            }
        })
            ->orderByDesc('sort')
            ->paginate($limit)
            ->toArray();

        return $res;
    }
    /**
     * 详情
     * @param id
     * @param is_play 是否发布
     */
    public function detail($id, $is_play = null)
    {
        $res = $this->where('id', $id)->where(function ($query) use ($is_play) {
            if ($is_play) {
                $query->where('is_play', $is_play);
            }
        })->where('is_del', 1)->first();
        if (empty($res)) {
            return false;
        }
        //获取对应数据记录
        if ($res['skip_way'] == 2) {
            $res['mode_data'] = $this->getModeData($res['mode'], $res['link']);
            $res['mode_name'] = $this->getBannerMode($res['mode']);
        }
        return $res;
    }
    /**
     *获取对应数据名称
     * $id
     */
    public function getModeData($mode, $id)
    {
        switch ($mode) {
            case 1: // 活动报名
                $model = new Activity();
                $data = $model->select('id', 'title')->where('is_del', 1)->where('id', $id)->first();
                break;
            case 2: // 答题活动
                $model = new AnswerActivity();
                $data = $model->select('id', 'title')->where('is_del', 1)->where('id', $id)->first();
                break;
            case 3: // 翻奖活动
                $model = new TurnActivity();
                $data = $model->select('id', 'title')->where('is_del', 1)->where('id', $id)->first();
                break;
            case 4: // 投票活动
                $model = new ContestActivity();
                $data = $model->select('id', 'title')->where('is_del', 1)->where('id', $id)->first();
                break;
            case 5: // 图片直播
                $model = new PictureLive();
                $data = $model->select('id', 'title')->where('is_del', 1)->where('id', $id)->first();
                break;
            case 6: // 新闻动态
                $model = new News();
                $data = $model->select('id', 'title')->where('is_del', 1)->where('id', $id)->first();
                break;
            case 7: // 场馆预约
                $model = new Reservation();
                $data = $model->select('id', 'name as title')->where('is_del', 1)->where('id', $id)->first();
                break;
            case 8: // 在线办证
                $data = [];
                break;
            default:
                $data = [];
        }
        return $data;
    }
}
